package com.salt.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @author HaiBo Kuang
 * @version 0.0.1 2021/3/30
 * @since 0.0.1
 */

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseData<T> {
    private ResponseStatus status;
    private T data;
    private String message;

    /**
     * 成功响应带data
     *
     * @param d   data
     * @param <T> 泛型
     * @return 结果
     */
    static <T> ResponseData<T> successData(T d) {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = ResponseStatus.OK;
        rd.message = "success";
        rd.data = d;
        return rd;
    }

    /**
     * 默认成功响应
     *
     * @param <T>
     * @return
     */
    static <T> ResponseData<T> success() {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = ResponseStatus.OK;
        rd.message = "success";
        return rd;
    }

    static <T> ResponseData<T> failData(String d) {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = ResponseStatus.INTERNAL_SERVER_ERROR;
        rd.message = d;
        return rd;
    }

    static <T> ResponseData<T> failData(ResponseStatus status) {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = status;
        rd.message = status.getDescription();
        return rd;
    }

    static <T> ResponseData<T> failData(ResponseStatus code, String errMsg) {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = code;
        rd.message = errMsg;
        return rd;
    }

    static <T> ResponseData<T> failData(ResponseStatus code, T data) {
        ResponseData<T> rd = new ResponseData<>();
        rd.status = code;
        rd.data = data;
        rd.message = code.getDescription();
        return rd;
    }

    public ResponseStatus getStatus() {
        return status;
    }

    public void setStatus(ResponseStatus status) {
        this.status = status;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

